package org.example.demo3;

import jakarta.servlet.http.HttpSession;
import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class IndexController {
//    用户列表
    private static List<UserForm> userFormList=new ArrayList<UserForm>();
//    初始化页面
    @RequestMapping("/index")
    public String index(UserForm user, HttpSession session, Model model){
        return "main";
    }
    @RequestMapping("/pre_register")
    public String pre_register(UserForm user, HttpSession session, Model model){
        return "register";
    }
    @RequestMapping("/pre_login")
    public String pre_login(UserForm user, HttpSession session, Model model){
        return "login";
    }
//    注册页面
    @RequestMapping("/register")
    public String register(@ModelAttribute("user") UserForm user,Model model){
//        注册逻辑
        if(user.getUpass().equals(user.getReupass())){
//            将用户添加到用户列表中
            userFormList.add(user);
//            消息框信息
            model.addAttribute("messageInfo","注册成功请登陆");
//            进入登陆页面
            return "login";
        }else {
//            消息框信息
            model.addAttribute("messageInfo","两次密码不一致,请重新输入");
//            停留在注册界面
            return "register";
        }
    }
//    登陆页面
    @RequestMapping("/login")
    public String login(@ModelAttribute("user") UserForm user, Model model) {
//        遍历用户列表寻找相同的用户
        for (UserForm u : userFormList) {
//            用户登陆逻辑
            if (u.getUname().equals(user.getUname()) && u.getUpass().equals(user.getUpass())) {
                return "success";
            }
        }
//        消息框信息
        model.addAttribute("messageInfo", "用户或密码错误");
        return "login";
    }
}
